<template>
    <div>
        <h1>用药管理</h1>
        <h3>登录人:{{ abc.someState.Dname}}</h3>
        <div>
            <tr>
                <td>医生名称</td>
                <td>
                    <input type="text" v-model="info.name">
                </td>
                <td align="right">科室</td>
                    <td>
                        <select v-model="info.tid">
                            <option value="">请选择</option>
                            <option :value="item.Tid" v-for="item in gettype">{{ item.Tname }}</option>
                        </select>
                    </td>
                <td>
                    <input type="button" value="查询" class="btn btn-success"  @click="getuse">
                    <input type="button" value="批量删除" class="btn btn-success"  @click="plDelAll">
                    <input type="button" value="批量修改" class="btn btn-success"  @click="plUpdateAll(false)">
                    <input type="button" value="导出" class="btn btn-info" @click="daochu">
                </td>
            </tr>
        </div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                <td>
                    <input type="checkbox" v-model="checked" @change="Quan">
                </td>
                <td>药品名称</td>
                <td>科室</td>
                <td>用药日期</td>
                <td>病人姓名</td>
                <td>病房号码</td>
                <td>用药状态</td>
                <td>配药医生</td>
                <td>操作</td>
            </tr>
            <tr v-for="item in data.list":key="item.Uid"> 
                <td><input type="checkbox" v-model="checkeds" :value="item.Uid" @change="Fan"></td>
                <td>{{ item.Uname }}</td>
                <td>{{ item.Tname }}</td>
                <td>{{ item.Udate.substring(0,10) }}</td> 
                <td>{{ item.Uxing }}</td>
                <td>{{ item.Uhao }}</td>
                <td>
                    <span style="color:green" v-if="item.Ustate==false">未开始</span>
                    <span style="color:red" v-if="item.Ustate==true">已完成</span>
                </td>
                <td>{{ item.Uuname }}</td>
                <td>
                    <input type="button" value="删除" class="btn btn-danger" @click="delUse(item.Uid)">
                    <input type="button" value="修改" class="btn btn-warning" @click="uptUse(item.Uid)">
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共{{ data.totalCount }}条</span>
        <span>每页{{ info.pageSize }}条</span>
        <span>当前{{ info.pageIndex }}/{{ data.totalPage }}页</span>

        <input type="button" value="首页" @click=Gopage(1)>
        <input type="button" value="上一页" @click=Gopage(info.pageIndex-1)>
        <input type="button"  :value="item" v-for="item in data.totalPage" @click=Gopage(item)>
        <input type="button" value="下一页" @click=Gopage(info.pageIndex+1)>
        <input type="button" value="尾页" @click=Gopage(data.totalPage)>
        <input type="number" v-model="info.pageIndex">
        <input type="button" value="跳转" class="btn btn-success" @click="Gopage(info.pageIndex)">
        <select v-model="info.pageSize" @click="getuse">
            <option :value="item" v-for="item in goSize">每页{{ item }}条</option>
        </select>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted}from 'vue'
import axios from 'axios'
import {useStore} from '@/stores/user'
import { useRoute,useRouter } from 'vue-router';
import { isTemplateExpression } from 'typescript';
const route = useRoute();
const router = useRouter();
const abc:any = useStore();
let checked:any=ref(false);
let checkeds:any=ref([]);
const daochu=()=>{
    location.href="https://localhost:7143/api/Use/ExportExcel?pageIndex=1&pageSize=2";
}

const plUpdateAll=(state:any)=>{
    if(checkeds.value.length==0)
{
    alert("必选一项");
    return;
}
    var f = confirm("确认修改吗?");
    if(f)
{
    axios({
        url:"/api/Use/UpdateState",
        method:"put",
        params:{
            checkeds:checkeds.value.toString(),
            state:state
        }
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("批量修改成功");
        getuse();
        checked.value=false;
        checkeds.value=false;
        return;
    }
    else
    {
        alert("批量修改失败");
        checked.value=false;
        
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
}

const plDelAll=()=>{
    if(checkeds.value.length==0)
{
    alert("必选一项");
    return;
}
    var f = confirm("确认删除吗?");
    if(f)
{
    axios({
        url:"/api/Use/UpdateDel",
        method:"put",
        params:{
            checkeds:checkeds.value.toString()
        }
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("批量删除成功");
        getuse();
        checked.value=false;
        return;
    }
    else
    {
        alert("批量删除失败");
        checked.value=false;
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
}

const Quan=()=>{
    if(checked.value)
{
    checkeds.value = data.list.map((item:any)=>{
        return item.Uid; 
    })
}
else
{
    checkeds.value = []; 
}
}
const Fan=()=>{
    if(checkeds.value.length==data.list.length)
{
    checked.value = true;
}
else
{
    checked.value = false;
}
}

const Gopage=(num:any)=>{
    if(num<1)
{
    info.pageIndex=1;
    return;
}
    if(num>data.totalPage)
{
    info.pageIndex = data.totalPage;
    return;
}
info.pageIndex=num;
getuse();
}

let goSize:any=ref([2,3,4,5]);
let info:any=reactive({
    pageIndex:1,
    pageSize:2,
    name:"",
    tid:""
});
let gettype:any=ref([]);

const BindType=()=>{
    axios({
        url:"/api/Use/BindType",
        method:"get",
    })
    .then((res)=>{
        console.log(res);
        gettype.value = res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}
const uptUse=(Uid:any)=>{
    router.push({
        path:"/UpdateUse",
        query:{
            id:Uid
        }
    })
}

const delUse=(Uid:any)=>{
    var f = confirm("确认删除吗?");
    if(f)
{
    axios({
        url:"/api/Use/DelUse",
        method:"delete",
        params:{
            id:Uid
        }
    })   
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("删除成功");
        getuse();
        return;
    }
    else
    {
        alert("删除失败");
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
}

let data:any=reactive({
    list:[],
    totalCount:"",
    totalPage:""
});

onMounted(()=>{
    if(abc.someState.Did)
{
    getuse();
    BindType();
}
else
{
    router.push("/Login");
}
})



const getuse=()=>{
    axios({
        url:"/api/Use/GetUse",
        method:"get",
        params:info
    })
    .then((res)=>{
        console.log(res);
        Object.assign(data,res.data);
    })
    .catch((err)=>{
        console.log(err);
    })
}


</script>

<style scoped>
    input
    {
        margin: 0 5px;
    }
</style>